package com.meteor.controller;

import com.meteor.framework.common.base.ResponseData;
import com.meteor.framework.common.utils.PageResult;
import com.meteor.framework.operatelog.annotations.OperateLog;
import com.meteor.framework.operatelog.enums.OperateTypeEnum;
import com.meteor.query.ComplaintQuery;
import com.meteor.query.MalfunctionQuery;
import com.meteor.service.ComplaintService;
import com.meteor.service.MalfunctionService;
import com.meteor.vo.ComplaintVO;
import com.meteor.vo.MalfunctionVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lynn
 * @date 2024/1/22
 */

@RestController
@RequestMapping("new/malfunction")
@Tag(name = "故障报备")
@AllArgsConstructor
public class MalfunctionController {

    private final MalfunctionService malfunctionService;


    @GetMapping("page")
    @Operation(summary = "分页查询")
//    @PreAuthorize("hasAuthority('sys:publicity:page')")
    public ResponseData page(@ParameterObject @Valid MalfunctionQuery query) {

        PageResult<MalfunctionVO> page = malfunctionService.pageMalfunctionVO(query);

        return ResponseData.success(page);
    }

    @GetMapping("allList")
    @Operation(summary = "全部查询")
    @PreAuthorize("hasAuthority('sys:publicity:all')")
    public ResponseData allList(MalfunctionQuery query) {

        List<MalfunctionVO> page = malfunctionService.getMalfunctionVOList(query);

        return ResponseData.success(page);
    }


    @GetMapping("{id}")
    @Operation(summary = "信息")
//    @PreAuthorize("hasAuthority('sys:publicity:info')")
    public ResponseData get(@PathVariable("id") String id) {
        return ResponseData.success("查询成功", malfunctionService.getMalfunction(id));
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
//    @PreAuthorize("hasAuthority('sys:publicity:save')")
    public ResponseData save(@RequestBody @Valid MalfunctionVO vo) {
        malfunctionService.save(vo);

        return ResponseData.success("新增成功");
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(type = OperateTypeEnum.UPDATE)
//    @PreAuthorize("hasAuthority('sys:publicity:update')")
    public ResponseData update(@RequestBody @Valid MalfunctionVO vo) {
        malfunctionService.update(vo);

        return ResponseData.success("修改成功");
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
//    @PreAuthorize("hasAuthority('sys:publicity:delete')")
    public ResponseData delete(@PathVariable("id") String id) {
        malfunctionService.delete(id);

        return ResponseData.success("删除成功");
    }

    @GetMapping("userMalfunction/{userId}")
    @Operation(summary = "我的维修")
    public ResponseData getUserMalfunction(@PathVariable("userId") String userId) {
        return ResponseData.success("查询成功", malfunctionService.getUserMalfunction(userId));
    }

}
